package com.itcast.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcast.reggie.pojo.Category;
import com.itcast.reggie.pojo.R;
import com.itcast.reggie.service.impl.FoodClassServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/category")
public class FoodClassController{
    @Autowired
    private FoodClassServiceImpl foodClassService;

    //查询所有数据
    @GetMapping("/page")
    public R<Page> SelectAll( int page,int pageSize){
        Page<Category> pages = new Page<Category>(page,pageSize);
        QueryWrapper<Category> wrapper = new QueryWrapper();
        wrapper.orderByAsc("sort");
        Page<Category> page1 = foodClassService.page(pages, wrapper);
        return R.success(page1);
    }

    //添加套餐与菜品分类
    @PostMapping
    public R<String> Save(@RequestBody Category category){
        foodClassService.save(category);
        return R.success("保存成功");
    }

    //修改分类
    @PutMapping
    public R<String> Update(@RequestBody Category category){
        foodClassService.updateById(category);
        return R.success("修改成功");
    }

    //删除分类
    @DeleteMapping
    public R<String> Delete(Long id){
        log.info("删除分类，id为：{}",id);
        foodClassService.remove(id);
        return R.success("删除成功");
    }

    //获取菜品类型,如:川菜,粤菜等等
    @GetMapping("/list")
    public R<List<Category>> GetList( Integer type){
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq(type != null,"type",type);
        wrapper.orderByAsc("type");
        wrapper.orderByAsc("create_time");
        List<Category> list = foodClassService.list(wrapper);
        return R.success(list);
    }
}
